#include "stdio.h"
#include "stdlib.h"
#include <string.h>
#include "StreamObs.h"
#include "BaseStation.h"
#include "test.h"
#include "datapack.h"
#include "initcfg.h"
#ifdef WIN32
#include <winsock2.h>
#else
#include <unistd.h>

#include <stdlib.h>
#include <sys/time.h>

#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>
#endif
#include "test.h"
int hex2buff(char *buff) {
    int nbyte = 0, bufflen = strlen(buff);
    long tmp = 0;
    char *tmpbuff=calloc(1024 * 2, sizeof(char));
    char *dump = calloc(3, sizeof(char));
    for (int i = 0; i < bufflen; i += 2) {
        dump[0] = buff[i];
        dump[1] = buff[i + 1];
        tmp = strtol(dump, NULL, 16);
        *(tmpbuff+(i/2)) = (char) tmp;
        nbyte++;
    }
    memset(buff, 0, sizeof(char) * (bufflen + 1));
    memcpy(buff,tmpbuff,sizeof(char)*bufflen);

    free(tmpbuff);
    free(dump);
    return nbyte;
}

char *replySat0() {
    // Compute Satellite Location.
    int i;
    char *msg=malloc(10240*sizeof(char));
    char *buf=Sat_m_getSatLocation(&DLY.SIT[0]); // 多线程的话，会不会同时算多个站？
    //printf("Sat Location: \n%s\n", buf);
    if (strlen(buf)>0){
	char sendbuf[9216]="";
	strcpy(sendbuf,StaComDamo.bufstatic);
	strcat(sendbuf,";");
	strcat(sendbuf,buf);
	    //printf("bufstatic:%s\n",StaComDamo.bufstatic);
        int x=splicingcommand(2,"/GNSS/SKY/RESP", sendbuf, strlen(sendbuf));
        strcpy(msg,StaComDamo.comdata);
        //printf("sendbuf:%s,sendbuflen:%d,comdata:%s\n",sendbuf,strlen(sendbuf),StaComDamo.comdata);
        //i=socket_send(concfg.clientSocketID, StaComDamo.comdata, x);
	//printf("i:%d,bufstatic:%s,comdata:%s\n",i,StaComDamo.bufstatic,StaComDamo.comdata);
    }
    free(buf);
    //printf("msg:%s\n",msg);
    return msg;
}

/*int main1(int argc,char ** argv) {
    initialization(argc,argv);
    printf("Return sat elev & azim\n\n");
    char *rawbuff[5]={"D3003D3FB1886042F50A189B00004D0E12580A07CA2ACDF30027DC06E101313F23129EA10CDA73189BFFEAFE5ACC2E00082828FC2E1BF3D5948F7DFFAA1909008B7184",
                   "D3002D3FC34A25DC97AC2000A163BC700B000C2F4557E21893ECD1266902C008083F5980453EC00000000001C0001B00374B57",
                   "D3003F4163D218206B0A6464003FFED00335963828B86C182039480420A40084B994586EA81353F0640FFF6F76BEBFE001A289B11DD10A4AB359761FFC5D50681D00C3D3F9",
                   "D3003D3FB348607D7D16189C0000311B41A816FA182D7B279E00BFFAB702E4E40F17F2A10D7C59189CFFDA58FDB5D8FFFB2773C523147F27B94DD6FFA9ACE8004FBCA6",
                   "D3003D414617BB00FFFFFFFA19A54A7C0003B3C5BAE4B3540086FAB47FA32BB0DEA45EEC0345D5BAA52C089C002EBA0F19DC62E6BFF80022B4000886103CF69075CF50",
    };//eph

    replySat = replySat0;
    Initialize();

    int nbyte=0;
    char *buff=calloc(sizeof(char),2049);
    memcpy(buff,rawbuff[0],sizeof(char)*strlen(rawbuff[0])+1);

    double stax[3]={-2258549.675,4996537.148,3247080.930};
    int mjd=0; double sod=0;
    mjd=59519; sod=10000.0;

    if (!(nbyte = hex2buff(buff)))
        return NULL;

    // 调用该函数即可，输入buff, stax, mjd, sod
    // replySat0 中返回了satbuf，格式为 "PRN1 elev azim\nPRN2 elev azim\n"

    // 如果 星历时间与计算时间 间隔超过2小时，误差会较大，不计算该卫星.
    // 如果 stax==0，不计算卫星位置；如果 time==0,计算当前时刻.
    StreamReader(buff, nbyte, stax, mjd, sod);

    free(buff);
    finish();
    return 0;
}*/

void SayHello(const char* s) {
   puts(s);
}

int maint() {
	printf("Return sat elev & azim\n\n");
	//Executionstatus_init();
	//initialization(argc,argv);
	int clientSocketID = 0, con=-1;
	//replySat = replySat0;
	Initialize();
	//初始化socket
	//init_socket();
	//初始化解析命令
	initcommand();
	//char memdata[10240] = "";
	int len = 0;
	//重置解析函数
	StaComDamo.onPack2=StreamReader;
	printf("END!\n");
	//Apipardata(&StaComDamo,buf,n);
	//while (1)
	//{
	    //printf("onnection succeeded:\nip:%s;port:%d;\n",concfg.ip, concfg.port);
		/*if (con <0) {
			concfg.clientSocketID = socket_new_tcp();
			con= socket_connect(concfg.clientSocketID, concfg.ip, concfg.port);
			if (con == -1)
			{
				printf("onnection failed!\nip:%s;port:%d;\n",concfg.ip, concfg.port);
				closesocket(concfg.clientSocketID);
				con = -1;
				#ifdef WIN32
				Sleep(1000 * 3);
				#else
				sleep(3);
				#endif
				continue;
			}
			printf("onnection succeeded:\nip:%s;port:%d;\n",concfg.ip, concfg.port);*/
			//int x=splicingcommand(0,concfg.channel_read, "aaa", 4);

           /* int sendx=socket_send(concfg.clientSocketID, StaComDamo.comdata,x);
			if(sendx<=0)
			{
				printf("error send !\n:send-data:%s\nsend-len:%d\n",StaComDamo.comdata,x);
				exit(1);
			}

		}*/
		/*int r = socket_recv(concfg.clientSocketID, memdata, 10240);
		if (r==0) {
			printf("onnection interupted!\n");
			#ifdef WIN32
			Sleep(1000 * 3);
			#else
			sleep(3);
			#endif
			closesocket(concfg.clientSocketID);
			con = -1;
			continue;
		}*/
		//int x=splicingcommand(0,concfg.channel_read, "aaa", 4);
		//Apipardata(&StaComDamo,memdata,10);
	//}
	//finish();
	//closecommand();
	return 0;
}


